{% load staticfiles %}
<script>
/**
 *  Function will call AJAX function to load the particular course preview.
 */
function ajax_popup_course_preview(course_id)
{
    var url = 'course_preview_modal';
    $.ajax( url, {
        data: {
           'course_id': course_id,
           'csrfmiddlewaretoken': '{{ csrf_token }}',
        },
        type: 'post',
        success: function(result) {
            // success code execution here
            $('#ajax_response_course_preview').html(result); // Update UI.
            $('#course_preview_modal').modal();               // Unhide view.
        },
        error: function(xhr,status,error) {
            // error code here
        },
        complete: function(xhr,status) {
            // completion code here
       }
    });
}
        
/**
 *  Function will call AJAX function to load the login modal.
 */
function ajax_popup_login()
{
    var url = 'login_modal';
            
    $.ajax( url, {
        data: {
           'csrfmiddlewaretoken': '{{ csrf_token }}',
        },
        type: 'post',
        success: function(result) {
            // success code execution here
            $('#ajax_response_login').html(result); // Update UI.
            $('#login_modal').modal();               // Unhide view.
        },
        error: function(xhr,status,error) {
            // error code here
        },
        complete: function(xhr,status) {
            // completion code here
        }
    });
}
        
/**
 *  Function will call AJAX function to load the register.
 */
function ajax_popup_register()
{
    var url = 'register_modal';
            
    $.ajax( url, {
        data: {
           'csrfmiddlewaretoken': '{{ csrf_token }}',
        },
        type: 'post',
        success: function(result) {
            // success code execution here
            $('#ajax_response_register').html(result); // Update UI.
            $('#register_modal').modal();               // Unhide view.
        },
        error: function(xhr,status,error) {
            // error code here
        },
        complete: function(xhr,status) {
            // completion code here
        }
    });
}

/**
 *  Sends a message.
 */
/**
 *  Function will call AJAX function to load the register.
 */
function ajax_send_message()
{
    $('#id_send_msg_btn').prop("disabled", true);
    $.ajax( 'save_contact_us_message', {
           data: {
                'csrfmiddlewaretoken': '{{ csrf_token }}',
                'name': $("#id_name").val(),
                'email': $("#id_email").val(),
                'phone': $("#id_phone").val(),
                'message': $("#id_message").val(),
                'captcha_0': $("#id_captcha_0").val(),
                'captcha_1': $("#id_captcha_1").val(),
           },
           type: 'post',
           success: function(result) {
                // success code execution here
                if (result.status == 'success')
                {
                    $('#id_contact_name_danger').html('');
                    $('#id_contact_email_danger').html('');
                    $('#id_contact_phone_danger').html('');
                    $('#id_contact_message_danger').html('');
                    $('#id_contact_captcha_danger').html('');
                    alert("Message sent!");
                    window.location = ''
                }
                else
                {
                    refresh_captcha();
                    var data = $.parseJSON(result.message)
                    var name_msg = '';
                    var email_msg = '';
                    var phone_msg = '';
                    var message_msg = '';
                    var captcha_msg = '';
                    for (var key in data) {
                        if (data.hasOwnProperty(key)) {
                            if (key == 'name')
                            {
                                name_msg = data[key];
                            }
                            if (key == 'email')
                            {
                                email_msg = data[key];
                            }
                            if (key == 'phone')
                            {
                                phone_msg = data[key];
                            }
                            if (key == 'message')
                            {
                                message_msg = data[key];
                            }
                            if (key == 'captcha')
                            {
                                captcha_msg = data[key];
                            }
                        }
                    }
           
                    $('#id_contact_name_danger').html(name_msg);
                    $('#id_contact_email_danger').html(email_msg);
                    $('#id_contact_phone_danger').html(phone_msg);
                    $('#id_contact_message_danger').html(message_msg);
                    $('#id_contact_captcha_danger').html(captcha_msg);
                }
           },
           error: function(xhr,status,error) {
                // error code here
           },
           complete: function(xhr,status) {
                // completion code here
                $('#id_send_msg_btn').prop("disabled", false);
           }
    });
}


/**
 * The following function will cause the captcha to refresh.
 */
function refresh_captcha()
{
    var url = location.protocol + "//" + window.location.hostname + ":" + location.port + "/captcha/refresh/";
    
    // Make the AJAX-call
    $.getJSON(url, {}, function(json) {
              //var $form = $('modal-body').parents('register_form');
              var $form = $('#contactForm');
              $form.find('input[name="captcha_0"]').val(json.key);
              $form.find('img.captcha').attr('src', json.image_url);
              });
              return false;
}

// Add refresh button after field (this can be done in the template as well)
$('img.captcha').after(
    $('<a href="#void" class="captcha-refresh">Refresh</a>')
);

// Click-handler for the refresh-link
$('.captcha-refresh').click(function(){
    refresh_captcha();
});
</script>